﻿#region 命名空间

using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.ExtNet;
using Sharp.Portal.Mvc;
using Sharp.Portal.Mvc.Form;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

#endregion

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    [AutomaticTemplate]
    public class UserController : Controller
    {

        public ActionResult Index(string containerId)
        {
            return new PartialViewResult
            {
                //ViewName = "Index",
                Model = new UserIndexViewModel(),
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }

        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetUsersPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }

        // GET: Manage/User/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            UserDTO userDto;
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                userDto = proxy.Channel.GetUserByKey(id.Value).Data;
            }
            if (userDto == null)
            {
                return HttpNotFound();
            }
            var vm = userDto.To<UserViewModel>();
            userDto.Password = string.Empty;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                vm.Person = proxy.Channel.GetEmployeeInfo(id.Value).To<EmployeeModel>();
            }
            if (vm.Person == null)
            {
                vm.Person = new EmployeeModel();
            }
            vm.Person.OrgIdDataSource = await OrgController.OrgTree();
            vm.UI.FieldsHandle = (formSections) =>
            {
                formSections.Hide(nameof(UserViewModel.Password), nameof(UserViewModel.ConfirmPassword));
            };

            return new PartialViewResult { Model = vm };
        }

        [Display(Name = "新增")]
        public async Task<ActionResult> Create()
        {
            var vm = new UserViewModel();
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                vm.RoleList = proxy.Channel.GetRoles();
            }
            if (vm.Person == null)
            {
                vm.Person = new EmployeeModel();
            }
            vm.Person.OrgIdDataSource = await OrgController.OrgTree();
            return new PartialViewResult { Model = vm };
        }


        [Display(Name = "编辑")]
        public async Task<ActionResult> Edit(int id)
        {
            UserViewModel vm;
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var user = proxy.Channel.GetUserByKey(id).Data;
                vm = user.To<UserViewModel>();
                vm.Password = "";
                vm.RoleList = proxy.Channel.GetRoles();
                vm.RoleIds = user.Roles.Select(x => x.Id);
            }


            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                vm.Person = proxy.Channel.GetEmployeeInfo(id).To<EmployeeModel>();
            }
            if (vm.Person == null)
            {
                vm.Person = new EmployeeModel();
            }
            vm.Person.OrgIdDataSource = await OrgController.OrgTree();

            return new PartialViewResult { Model = vm };
        }


        public ActionResult Save(UserViewModel user)
        {
            UserDTO dto = user.To<UserDTO>();
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                res = proxy.Channel.SaveUser(dto);
            }

            return this.JsonNet(res);
        }

        [System.Web.Mvc.HttpPost]
        [Display(Name = "删除")]
        public ActionResult Delete(int[] ids)
        {
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                res = proxy.Channel.DeleteUsers(ids);
            }

            return this.JsonNet(res);
        }
    }
}